function pos_pwd_widgets(instance, module){
    var QWeb = instance.web.qweb,
    _t = instance.web._t;

    var round_pr = instance.web.round_precision

	module.ScreenWidget.include({
		show_rightheader:   true,
        show: function(){
            this._super();
            var self = this;
            this.pos_widget.set_rightheader_visible(this.show_rightheader);

        },

    });

	module.PaymentScreenWidget.include({
        show: function(){
            this._super();
            var self = this;
            this.add_action_button({
                    label: _t('Special Discount'),
                    icon: '/point_of_sale/static/src/img/icons/png48/help.png',
                    click: function(){  
                    	self.pos_widget.screen_selector.set_current_screen('specialdiscount');

                    },
                });
        },

        update_payment_summary: function() {
        	this._super();
        	var currentOrder = this.pos.get('selectedOrder');
            var paidTotal = currentOrder.getPaidTotal();
            var dueTotal = currentOrder.getTotalTaxIncluded();
            var specialDiscount = currentOrder.SpecialDiscountAmt || 30;
            
            var afterDiscountTotal = dueTotal - specialDiscount;
            
        	
        	var remaining = afterDiscountTotal > paidTotal ? afterDiscountTotal - paidTotal: 0;

        	var change = paidTotal > afterDiscountTotal ? paidTotal - afterDiscountTotal : 0;
        	
//        	currentOrder.SpecialDiscountAmt = 342;
            this.$('.special-discount-amount').html(this.format_currency(specialDiscount));
            this.$('.payment-remaining').html(this.format_currency(remaining));
            this.$('.payment-change').html(this.format_currency(change));
        },

        is_paid: function(){
            var currentOrder = this.pos.get('selectedOrder');
            var specialDiscount = currentOrder.SpecialDiscountAmt || 30;
            return (currentOrder.getTotalTaxIncluded() < 0.000001 
                   || currentOrder.getPaidTotal() + 0.000001 >= (currentOrder.getTotalTaxIncluded() - specialDiscount));

        },
    });

	module.PosWidget.include({

        init: function(parent, options) { 
            this._super(parent, options);
            this.rightheader_visible = true;
        },
		
        set_rightheader_visible: function(visible){
            if(visible !== this.rightheader_visible){
                this.rightheader_visible = visible;
                if(visible){
                    this.$('.pos-rightheader').removeClass('oe_hidden');
                }else{
                    this.$('.pos-rightheader').addClass('oe_hidden');
                }
            }
        },
		
        build_widgets: function() {
            var self = this;

            // --------  Screens ---------

            this.product_screen = new module.ProductScreenWidget(this,{});
            this.product_screen.appendTo(this.$('.screens'));

            this.receipt_screen = new module.ReceiptScreenWidget(this, {});
            this.receipt_screen.appendTo(this.$('.screens'));

            this.payment_screen = new module.PaymentScreenWidget(this, {});
            this.payment_screen.appendTo(this.$('.screens'));

            this.clientlist_screen = new module.ClientListScreenWidget(this, {});
            this.clientlist_screen.appendTo(this.$('.screens'));

            this.scale_screen = new module.ScaleScreenWidget(this,{});
            this.scale_screen.appendTo(this.$('.screens'));

            this.special_discount_screen = new module.SpecialDiscountScreenWidget(this,{});
            this.special_discount_screen.appendTo(this.$('.screens'));

            // --------  Popups ---------

            this.error_popup = new module.ErrorPopupWidget(this, {});
            this.error_popup.appendTo(this.$el);

            this.error_barcode_popup = new module.ErrorBarcodePopupWidget(this, {});
            this.error_barcode_popup.appendTo(this.$el);

            this.error_traceback_popup = new module.ErrorTracebackPopupWidget(this,{});
            this.error_traceback_popup.appendTo(this.$el);

            this.confirm_popup = new module.ConfirmPopupWidget(this,{});
            this.confirm_popup.appendTo(this.$el);

            this.unsent_orders_popup = new module.UnsentOrdersPopupWidget(this,{});
            this.unsent_orders_popup.appendTo(this.$el);

            // --------  Misc ---------

            this.close_button = new module.HeaderButtonWidget(this,{
                label: _t('Close'),
                action: function(){ 
                    var self = this;
                    if (!this.confirmed) {
                        this.$el.addClass('confirm');
                        this.$el.text(_t('Confirm'));
                        this.confirmed = setTimeout(function(){
                            self.$el.removeClass('confirm');
                            self.$el.text(_t('Close'));
                            self.confirmed = false;
                        },2000);
                    } else {
                        clearTimeout(this.confirmed);
                        this.pos_widget.close();
                    }
                },
            });
            this.close_button.appendTo(this.$('.pos-rightheader'));

            this.notification = new module.SynchNotificationWidget(this,{});
            this.notification.appendTo(this.$('.pos-rightheader'));

            if(this.pos.config.use_proxy){
                this.proxy_status = new module.ProxyStatusWidget(this,{});
                this.proxy_status.appendTo(this.$('.pos-rightheader'));
            }

            this.username   = new module.UsernameWidget(this,{});
            this.username.replace(this.$('.placeholder-UsernameWidget'));

            this.action_bar = new module.ActionBarWidget(this);
            this.action_bar.replace(this.$(".placeholder-RightActionBar"));

            this.paypad = new module.PaypadWidget(this, {});
            this.paypad.replace(this.$('.placeholder-PaypadWidget'));

            this.numpad = new module.NumpadWidget(this);
            this.numpad.replace(this.$('.placeholder-NumpadWidget'));

            this.order_widget = new module.OrderWidget(this, {});
            this.order_widget.replace(this.$('.placeholder-OrderWidget'));

            this.onscreen_keyboard = new module.OnscreenKeyboardWidget(this, {
                'keyboard_model': 'simple'
            });
            this.onscreen_keyboard.replace(this.$('.placeholder-OnscreenKeyboardWidget'));

            // --------  Screen Selector ---------

            this.screen_selector = new module.ScreenSelector({
                pos: this.pos,
                screen_set:{
                    'products': this.product_screen,
                    'payment' : this.payment_screen,
                    'scale':    this.scale_screen,
                    'receipt' : this.receipt_screen,
                    'clientlist': this.clientlist_screen,
                    'specialdiscount': this.special_discount_screen,
                },
                popup_set:{
                    'error': this.error_popup,
                    'error-barcode': this.error_barcode_popup,
                    'error-traceback': this.error_traceback_popup,
                    'confirm': this.confirm_popup,
                    'unsent-orders': this.unsent_orders_popup,
                },
                default_screen: 'products',
                default_mode: 'cashier',
            });

            if(this.pos.debug){
                this.debug_widget = new module.DebugWidget(this);
                this.debug_widget.appendTo(this.$('.pos-content'));
            }

            this.disable_rubberbanding();

        },

    });

//	module.Order = module.Order.extend({
//        initialize: function(attributes){
//        	this._super(attributes);
//        	this.SpecialDiscountAmt = 151;
//            return this;
//        },
//        set_special_discount_amt: function(amt){
//        	this.SpecialDiscountAmt = amt;
//        },
//        get_special_discount_amt: function(){
//            return this.SpecialDiscountAmt;
//        },
//    });

    module.SpecialDiscountScreenWidget = module.ScreenWidget.extend({
        template: 'SpecialDiscountScreenWidget',

        init: function(parent, options){
            this._super(parent, options);
        },

        show_leftpane: false,
        auto_back: true,
        show_rightheader: false,
 
        show: function(){
            this._super();
            var self = this;

            this.add_action_button({
                    label: _t('Back'),
                    icon: '/point_of_sale/static/src/img/icons/png48/go-previous.png',
                    click: function(){  
                    	self.pos_widget.screen_selector.back();
                    },
                });
            this.add_action_button({
                label: _t('Validate'),
                name: 'validationsd',
                icon: '/point_of_sale/static/src/img/icons/png48/validate.png',
                click: function(){  
                	self.validate_order();

                },
            });

            this.update_payment_summary();

        },

        update_payment_summary: function() {
            var currentOrder = this.pos.get('selectedOrder');
            var dueTotal = currentOrder.getTotalTaxIncluded();
            this.$('.payment-due-total').html(this.format_currency(dueTotal));
            this.$('.specialdiscount-input')[0].value = 0;
            this.$('.specialdiscount-input').focus();
            this.$('.sdpassword-input')[0].value = '';
        },

        is_paid: function(){
            return true;

        },

        validate_order: function(options) {
            var self = this;
            options = options || {};
            var currentOrder = this.pos.get('selectedOrder');
            var dueTotal = currentOrder.getTotalTaxIncluded();

            var Valuesd = this.$('.specialdiscount-input')[0].value
            var sdPassword = this.$('.sdpassword-input')[0].value

            if(sdPassword != '123abc'){
                this.pos_widget.screen_selector.show_popup('error',{
                    'message': _t('Wrong Password Input'),
                    'comment': _t('The Password your Entered is Wrong'),
                });
                return;
            }
            
            if(Valuesd < 0){
                this.pos_widget.screen_selector.show_popup('error',{
                    'message': _t('Negative Value'),
                    'comment': _t('The Special Discount Input cannot be Negative'),
                });
                return;
            }
            if(Valuesd > dueTotal){
                this.pos_widget.screen_selector.show_popup('error',{
                    'message': _t('Special Discount > Amount'),
                    'comment': _t('The Special Discount Input Cannot More than Actual Value'),
                });
                return;
            }
            currentOrder.SpecialDiscountAmt = Valuesd;
            
//            this.pos_widget.screen_selector.show_popup('error',{
//                'message': _t('Special Discount > Amount'),
//                'comment': _t('The Special Discount Input Cannot More than Actual Value' + currentOrder.SpecialDiscountAmt),
//            });
//            return;
            
            self.pos_widget.screen_selector.back();
        },
        
        close: function(){
            this._super();
        },
    });
 

}

(function(){
    var _super = window.openerp.point_of_sale;
    window.openerp.point_of_sale = function(instance){
        _super(instance);
        var module = instance.point_of_sale;
        pos_pwd_widgets(instance, module);

        $('<link rel="stylesheet" href="/pos_enhanced/static/src/css/pos.css"/>').appendTo($("head"))
    }
})()

